package com.nuckly.news.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.nuckly.news.R
import com.nuckly.news.common.addFragment
import com.nuckly.news.data.News
import com.nuckly.news.databinding.ActivityNewsBinding
import com.nuckly.news.ui.fragment.NewsFragment

class NewsActivity : AppCompatActivity() {

    private lateinit var binding: ActivityNewsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNewsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val news = intent.getParcelableExtra<News>(EXTRA_NEWS) as News
        addNewsFragment(news)
    }

    private fun addNewsFragment(news: News) {
        addFragment(R.id.fragment_container, NewsFragment.newIntent(news))
    }

    companion object {

        private const val EXTRA_NEWS = "com.nuckly.news.ui.activity.news"

        fun startActivity(context: Context, news: News) {
            val intent = Intent(context, NewsActivity::class.java).apply {
                putExtra(EXTRA_NEWS, news)
            }
            context.startActivity(intent)
        }

    }
}